<?php
    $pg_source = "http://git.oschina.net/fonny/php-snippets";
    $pg_name = "Post_checker";
    $pg_version = "2014-8-19";
    $pg_desc = "基本防XSS表单验证";

    $name = $passwd = $email = $gender = "";
    $nameErr = $passwdErr = $emailErr = $genderErr = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST['name'])) {
            $nameErr = "不可为空";
        } else {
            $name = test_input($_POST['name']);
        }

        if (empty($_POST['passwd'])) {
            $passwdErr = "不可为空";
        } else {
            $passwd = test_input($_POST['passwd']);
        }

        if (empty($_POST['email'])) {
            $emailErr = "不可为空";
        } else {
            $email = test_input($_POST['email']);
        }

        if (empty($_POST['gender'])) {
            $genderErr = "不可为空";
        } else {
            $gender = test_input($_POST['gender']);
        }
    }

    function echoCheckerUrl()
    {
        echo htmlspecialchars($_SERVER["PHP_SELF"]);
    }

    function test_input($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);

        return $data;
    }

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">

    <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
    Remove this if you use the .htaccess -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <title>post_checker</title>
    <meta name="description" content="">
    <meta name="author" content="Fonny">
    <meta name="viewport" content="width=device-width; initial-scale=1.0">
    <!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="apple-touch-icon" href="/apple-touch-icon.png">
</head>

<body>
<div>
    <header>
        <h1 id="pg_name"><?php echo($pg_name); ?></h1>

        <p id="pg_version">v<?php echo($pg_version); ?></p>

        <p id="pg_desc"><?php echo($pg_desc); ?></p>

        <h3>
            <a href="<?php echo($pg_source); ?>">Source</a>
        </h3>
    </header>
    <div id="div_form_container">
        <form action="<?php echoCheckerUrl(); ?>" method="post" accept-charset="utf-8">
            <p>
                <label>Name:<br>
                    <input type="text" name="name" class="input_text" maxlength="24"/>
                </label><span class="error">* <?php echo $nameErr; ?></span>
            </p>

            <p>
                <label>Password:<br>
                    <input type="password" name="passwd" class="input_text" maxlength="24"/>
                </label><span class="error">* <?php echo $passwdErr; ?></span>
            </p>

            <p>
                <label>Email:<br>
                    <input type="text" name="email" class="input_text" maxlength="24"/>
                </label>
                <span class="error">* <?php echo $emailErr; ?></span>
            </p>

            <p>Gender: <br>
                <label>
                    <input type="radio" name="gender" value="female">
                </label>Female
                <label>
                    <input type="radio" name="gender" value="male">
                </label>Male
                <span class="error">* <?php echo $genderErr; ?></span>
            </p>

            <p>
                <label>Comments:<br>
                    <textarea name="comment" wrap="soft" maxlength="250" rows="5"></textarea>
                </label>
            </p>

            <p><input type="submit" name="submit" value="提交 &rarr;"/></p>
        </form>
    </div>

    <footer>
        <p>
            &copy; Copyright by Fonny
        </p>
    </footer>
</div>
</body>
</html>
